/*
 ============================================================================
 Name		: CConfiguration.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CCConfiguration implementation
 ============================================================================
 */

#include "Globals.h"

#include "Configuration.h"
#include "EIKENV.H" //for CEikonEnv
//for getting the drive letter where the application is installed
#include "FRamI_UIAppUi.h" 
#include "FRamI_UIApplication.h"
#include <bautils.h> //for BaflUtils, link against: bafl.lib
#include <COEMAIN.H> //for CCoeEnv, link against: cone.lib

#include <s32file.h> //kniznica pre pracu so subormi - potrebna kniznica efsrv.lib

CConfiguration::CConfiguration()
/*iPhone(0, 50),
iMail(0,50),
iPin(0, 50)*/
	{
	// No implementation required
	}

CConfiguration::~CConfiguration()
	{
	}

CConfiguration* CConfiguration::NewLC()
	{
	CConfiguration* self = new (ELeave) CConfiguration();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CConfiguration* CConfiguration::NewL()
	{
	CConfiguration* self = CConfiguration::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CConfiguration::ConstructL()
	{

	}

void CConfiguration::SaveSettingToFileL()
	{
		const TInt KPathNameLength = 100;
		TBuf< KPathNameLength> filePath;
	 
		filePath.Copy(KHomeDir);
		filePath.Append(KConfigurationFile);
		
		//check if the file exists
		if (!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),filePath)) // file does not exist - create it
		{
			//create empty file
			RFile file;
			User::LeaveIfError(file.Create(CCoeEnv::Static()->FsSession(),filePath,EFileWrite));
			file.Close();
	    }
		
		//before opening file we need a fileserver connection
		RFs fileserver;
		User::LeaveIfError(fileserver.Connect());
		CleanupClosePushL(fileserver);
				 
		RFileWriteStream writeStream;
		writeStream.PushL();// reader on cleanup stack
		User::LeaveIfError(writeStream.Open(fileserver, filePath, EFileWrite));
		
		
		//RFile file;
		
		//file.Open(fileserver, filePath, EFileWrite);
		
		/*_LIT(KTemp, "test");
		TBufC<50> buf(KTemp);
		TPtr ptr = buf.Des();
		
		RDebug::Print(KTemp);
		RDebug::Print(ptr);
		RDebug::Print(buf);*/
		
		writeStream.WriteInt16L(iLoudNotification);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iHiddenNotification);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iSecurityLevel);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iPhone.Length());
		writeStream.WriteL(_L("\n"));
		writeStream.WriteL(iPhone);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iMail.Length());
		writeStream.WriteL(_L("\n"));
		writeStream.WriteL(iMail);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iPin.Length());
		writeStream.WriteL(_L("\n"));
		writeStream.WriteL(iPin);
		writeStream.WriteL(_L("\n"));
		writeStream.WriteInt16L(iSoundFile.Length());
		writeStream.WriteL(_L("\n"));
		writeStream.WriteL(iSoundFile);
		
		writeStream.CommitL();
				 
		writeStream.Close();
					
		CleanupStack::PopAndDestroy(&writeStream); 
					
		//closing fileserver connection
		fileserver.Close();
		CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CConfiguration::LoadSettingFromFileL()
	{
			const TInt KPathNameLength = 100;
			TBuf< KPathNameLength > pathWithoutDrive;
			TBuf< 2 > appDrive;
			TBuf< KPathNameLength> filePath;
		 
			filePath.Copy(KHomeDir);
			filePath.Append(KConfigurationFile);
			
			//check if file exists
			if (!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),filePath)) // file does not exist - create it
			{
				//set default settings
				iLoudNotification = 0;
				iHiddenNotification = 0;
				iPhone = _L("");
				iMail = _L("");
				iPin = _L("0000");
				iSoundFile = KDefaultSound;
				iSecurityLevel = 0;

				return;
		    }
			
			//before opening file we need a fileserver connection
			RFs fileserver;
			User::LeaveIfError(fileserver.Connect());
			CleanupClosePushL(fileserver);
				 
			RFileReadStream readStream;
			readStream.PushL();// reader on cleanup stack
			User::LeaveIfError(readStream.Open(fileserver, filePath, EFileRead));
				
			iLoudNotification = readStream.ReadInt16L();
			readStream.ReadL(2);
			iHiddenNotification = readStream.ReadInt16L();
			readStream.ReadL(2);
			iSecurityLevel = readStream.ReadInt16L();
			readStream.ReadL(2);
			TInt l = readStream.ReadInt16L();
			readStream.ReadL(2);
			readStream.ReadL(iPhone, l);
			readStream.ReadL(2);
			l = readStream.ReadInt16L();
			readStream.ReadL(2);
			readStream.ReadL(iMail, l);
			readStream.ReadL(2);
			l = readStream.ReadInt16L();
			readStream.ReadL(2);
			readStream.ReadL(iPin, l);
			readStream.ReadL(2);
			l = readStream.ReadInt16L();
			readStream.ReadL(2);
			readStream.ReadL(iSoundFile, l);
			
			readStream.Close();
			
			CleanupStack::PopAndDestroy(&readStream); 
							
			//closing fileserver connection
			fileserver.Close();
			CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CConfiguration::UpdateSettings( const TInt aLoudNotification,
									 const TInt aHiddenNotification,
									 const TInt aSecurityLevel,
									 const TDes &aPhone,
									 const TDes &aMail,
									 const TDes &aPin,
									 const TDes &aSoundFile)
	{
		iLoudNotification = aLoudNotification;
		iHiddenNotification = aHiddenNotification;
		iSecurityLevel = aSecurityLevel;
		iPhone = aPhone;
		iMail = aMail;
		iPin = aPin;
		iSoundFile = aSoundFile;
	}

void CConfiguration::SetSoundFile( const TDesC &aSoundFile)
	{
		iSoundFile = aSoundFile;
	}

TInt CConfiguration::IsOk()
	{
		//ak ma byt spusteny alarm je potrebne nastavit cestu k suboru
		if(iLoudNotification == 1)
		{
			if(iSoundFile.Length() > 0)
			{
				TParse p;
				p.Set(iSoundFile, NULL, NULL);
				TPtrC ext = p.Ext();
				RDebug::Print(ext);
				if(ext.CompareC(_L(".mp3")) != 0 &&  ext.CompareC(_L(".wav")) !=0 && ext.CompareC(_L(".amr")) != 0
				   && ext.CompareC(_L(".wma")) != 0 && ext.CompareC(_L(".midi")) != 0	
				   )
					return 2;
			}
			else
				return 1;
		}
		
		if(iHiddenNotification == 1 || iHiddenNotification == 2)
		{
			if(iPhone.Length() < 10)
				return 3;
		}
		else
			if(iHiddenNotification == 3)
			{
				if(iMail.Length() > 0)
				{
					if(iMail.Match(_L("*@*.*")) == KErrNotFound)
						return 5;
				}
				else
					return 4;
			}
		
		if(iPin.Length() == 0)
			return 6;
		
		return 0;
	}
